
 /*
 新闻管理页
author:
time : 2021年10月26日
 */ 
<template>
  <div>
    <!-- 搜索框 -->
    <div style="margin-top: 15px">
      <el-input placeholder="请输入内容" v-model="input" class="input">
        <el-button
          class="btn"
          slot="append"
          icon="el-icon-search"
          @click="search()"
        ></el-button>
      </el-input>
      <button class="add" type="buttom" @click="add()">
        <i class="icon el-icon-plus"></i> 增加新闻
      </button>
    </div>
    <!-- 表格 -->
    <div class="content_table">
      <el-table :data="adminList" style="width: 99%">
        <el-table-column label="ID" :width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <!-- <span>{{ scope.row.news_image }}</span> -->
            <img :src="`${imageName}${scope.row.news_image}`" alt="">
          </template>
        </el-table-column>
        <el-table-column label="手机名称">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
          
              <span>{{ scope.row.news_name }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini"  type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm
                  confirm-button-text='确定'
                  cancel-button-text='取消'
                  icon="el-icon-info"
                  icon-color="red"
                  title="确定删除这一条数据吗？"
                  @onConfirm="del(scope.row.id)"
                >
            <el-button slot="reference" size="mini" type="danger" >删除</el-button>
          </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-size="pageSize"
        layout="total, prev, pager, next,jumper"
        :total="count"
      
      >
      </el-pagination>
    </div>

    <!-- 模态框 -->
    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="上传图片" :label-width="formLabelWidth">
          <!-- <input style="width: 65%" type="file" @change="addImg($event)" />
            <span id="photo"
              ><img :src="`${imageName}${imageUrl}${form.news_image}`"
            /></span> -->
            <el-upload
              class="upload-demo"
              action="http://192.168.3.180:7001/api/uploadPicture"
              :on-preview="handlePreview"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
        </el-form-item>
        <el-form-item label="手机名称" :label-width="formLabelWidth">
          <el-input v-model="form.news_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机型号id" :label-width="formLabelWidth">
          <el-input v-model="form.pid" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="sbumit()">提 交</el-button>
      </div>
    </el-dialog>
   
  </div>
</template>

<script>
 
import {
  getNews,
  postNews,
  putNews,
  deleteNews,
  postupLoad
} from "@/api/table";

export default {
  components:{
    // Page
  },
  data() {
    return {
      input: "",
      value:'100',
      // 是否启用,默认启用
      value1: true,
      adminList: [],
      //查询关键字
      keyName: "",
     // 数据库总页数
      count: 0,
      // 当前页数
      page: 1,
      // 每页显示几条数据
      pageSize: 5,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        id: null,
        news_name: "",
        news_image: "",
        pid:"",
      },
        fileList: [],
      // 模态框输入长度
      formLabelWidth: "120px",
      // 判断是新增加还是修改,默认是添加
      isEdit: false,
      imageUrl: "",
      imageName:"http://192.168.3.180:7001/"
    };
  },

  created() {
    this.getDate();
  },

  methods: {
     
      handlePreview(file) {
        console.log(file.response);
        this.form.news_image =file.response.data;
      },
    handleCurrentChange(){
      this.getDate()
    },
    // 获取新闻数据的方法
    getDate() {
      console.log(this.page);
      const params = {
        page: this.page,
        number: this.pageSize,
        name: this.keyName,
        
      };
      getNews(params).then((res) => {
        // console.log(res);
        if (res.code === 0) {
          this.adminList = [...res.data];
          this.count = res.count;
        }  
       if (res.code === 4004) {
          this.adminList = res.data;
          this.count = res.count;
          this.page=res.page;
        } 
      });
    },
    // // 上传/编辑图片
    // addImg(event){
    //   const i = event.target.files[0];
    //   const formdata = new FormData();
    //   formdata.append("one",i);
    //   postupLoad(formdata).then((res)=>{
    //     console.log(res.data);
    //     if(res.code ===0){
    //       this.form.news_image = res.data;
    //       console.log(this.form.news_image);
    //     }
    //   })
    // },
    // 根据关键字查询
    search() {
      this.keyName = this.input;
      this.getDate();
    },
    // 新增新闻
    add() {
      // 显示模态框
      this.dialogFormVisible = true;
      this.isEdit = true;
    },
    // 编辑新闻
    edit(data) {
      // 显示模态框
      this.dialogFormVisible = true;
      this.isEdit = false;
      this.form.id = data.id;
      this.form.news_name = data.news_name;
      this.form.news_image = data.news_image;
      this.form.pid = data.pid;

    },
    // 删除新闻
    del(id) {
      // this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   type: "warning",
      // })
      //   .then(() => {
          deleteNews(id).then((res) => {
            if (res.code === 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 删除后重新渲染
              this.getDate();
            }
          });
        // })
        // .catch(() => {
        //   this.$message({
        //     type: "info",
        //     message: "已取消删除",
        //   });
        // });
    },
    // 点击提交
    sbumit() {
    
      // 添加到数据库
      let params = {};
      if (this.isEdit) {
        params = { ...this.form };
        console.log(params);
        postNews(params).then((res) => {
          if (res.code === 0) {
            this.$message({
              message: "添加新闻成功",
              type: "success",
            });
            // 添加成功后关闭模态框
            this.dialogFormVisible = false;
            // 把新添加的数据渲染到页面
            this.getDate();
          } else {
            this.$message.error("添加新闻失败");
          }
        });
      } else {
        params.id = this.form.id;
        params.news_name = this.form.news_name;
        params.news_image = this.form.news_image;
        params.pid = this.form.pid;
      
        putNews(params).then((res) => {
          if (res.code === 0) {
            this.$message({
              message: "编辑新闻成功",
              type: "success",
            });
            // 添加成功后关闭模态框
            this.dialogFormVisible = false;
            // 把新添加的数据渲染到页面
            this.getDate();
          } else {
            this.$message.error("编辑新闻失败");
          }
        });
      }
    },
    pageChange(page){
      console.log(page);
      this.page = page;
      this.getDate();
    },
    close(){
      this.dialogFormVisible = false;
      this.form.news_name =""
      this.form.news_image = ""
    }
  },
};
</script>

<style lang="scss" scoped>
 
.fa {
  margin-right: 5px;
}

.bread {
  width: 100%;
  height: 30px;
  padding: 10px 0;
  display: flex;
  padding-left: 10px;
  align-items: center;
  .el-breadcrumb {
    line-height: 30px;
    width: 100%;
    background-color: #fff;
  }
}
.input {
  background-color: #fff;
  width: 300px;
   margin: 30px 0px 10px 90px;
}
.add {
  background-color: rgb(133, 190, 228);
  color: white;
  border: none;
  line-height: 38px;
  border-radius: 5px;
  margin-left: 20px;
}
.content_table {
  width: 90%;
  margin: 10px auto;
}
th {
  width: 30%;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  img{
    width: 70px;
    height: 60px;
  }
  .block{
      margin: 20px 90px;
    display: inline-block;
  }
  .el-button{
  margin-right: 10px;
}
</style> 